package com.ftg.learn.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author wangty
 * @date 2022/10/21
 */
@ApiModel
@Data
public class ResultJson {
    @ApiModelProperty("返回状态码")
    int code;

    @ApiModelProperty("返回文字信息")
    String msg;

    @ApiModelProperty("返回的数据")
    Object data;

    /**
     * 成功方法，返回信息
     * @param data 传递给前台的参数
     * @return ResultJson
     */
    public static ResultJson OK(Object data){
        ResultJson r = new ResultJson();
        r.setCode(ResultJsonEnum.OK.getCode());
        r.setMsg(ResultJsonEnum.OK.getMsg());
        r.setData(data);
        return r;
    }

    /**
     * 失败方法，返回信息
     * @return ResultJson
     */
    public static ResultJson FAILURE(){
        ResultJson r = new ResultJson();
        r.setCode(ResultJsonEnum.FAILURE.getCode());
        r.setMsg(ResultJsonEnum.FAILURE.getMsg());
        r.setData(null);
        return r;
    }

    /**
     * 构建方法，用来调用enum
     * @param ts ResultJsonEnum
     * @return ResultJson
     */
    public static ResultJson builder(ResultJsonEnum ts){
        ResultJson r = new ResultJson();
        r.setCode(ts.getCode());
        r.setMsg(ts.getMsg());
        r.setData(null);
        return r;
    }
}
